package sealte.hvpms.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import sealte.hvpms.pojo.Poll;
import sealte.hvpms.pojo.PollOption;
import sealte.hvpms.pojo.RuleLocks;

public class HVPMSTools {
	public static boolean isNum(String val) {
        return val == null || "".equals(val) ? false : val.matches("^[0-9]*$");
    }
	
	public static boolean exportExcel(List<RuleLocks> list,String[] tableHead,OutputStream os){
		boolean flag = false;
		WritableWorkbook book = null;
		try{
			book = Workbook.createWorkbook(os);
			WritableSheet sheet = book.createSheet("第一页", 0); 
			Label tableHead_name = new Label(0,0,"投票标题：");
			sheet.addCell(tableHead_name);
			Label tableHead_content = new Label(1,0,tableHead[0]);
			sheet.addCell(tableHead_content);
			for(int i=1;i<tableHead.length;i++){
				Label th_content = new Label(i-1,1,tableHead[i]);
				sheet.addCell(th_content);
			}
			Iterator<RuleLocks> iter = list.iterator();
			int j = 0;
			while(iter.hasNext()){
				RuleLocks rl = iter.next();
				Label content_num = new Label(0,j+2,String.valueOf(j+1));
				sheet.addCell(content_num);
				Label content = new Label(1,j+2,rl.getLocks());
				sheet.addCell(content);
				j++;
			}
			book.write();
			flag = true;
		}catch(WriteException e){
			System.out.println("写入Excel出错！！"+e);
		}catch(IOException e){
			System.out.println("导出Excel出错！！"+e);
		}finally{
			try{
				if(book!=null){
					book.close();
					book = null;
				}
			}catch(WriteException e){
				e.printStackTrace();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		return flag;
	}
	
	public static boolean exportExcel(Map<Integer,Poll> map1,Map<Integer,List<PollOption>> map2,String[] tableHead,OutputStream os){
		boolean flag = false;
		WritableWorkbook book = null;
		try{
			book = Workbook.createWorkbook(os);
			WritableSheet sheet = book.createSheet("第一页", 0);
			
			Poll poll = null;
			List<PollOption> all = null;
			Iterator<PollOption> iter = null;
			PollOption po = null;
			int k = 0;
			for(int i=0;i<map1.size();i++){
				poll = map1.get(i);
				all = map2.get(i);
				iter = all.iterator();
				
				sheet.addCell(new Label(0,k,tableHead[0]));
				sheet.addCell(new Label(1,k,poll.getName()));
				sheet.addCell(new Label(2,k,tableHead[1]));
				sheet.addCell(new Label(3,k,String.valueOf(poll.getAll_polls())));
				sheet.addCell(new Label(0,k+1,tableHead[2]));
				sheet.addCell(new Label(1,k+1,tableHead[3]));
				sheet.addCell(new Label(2,k+1,tableHead[4]));
				
				int j = 1;
				while(iter.hasNext()){
					po = iter.next();
					sheet.addCell(new Label(0,k+1+j,String.valueOf(j)));
					sheet.addCell(new Label(1,k+1+j,po.getContent()));
					sheet.addCell(new Label(2,k+1+j,String.valueOf(po.getCounter())+"("+po.getPercentage()+")"));
					j++;
				}
				k += 3 + all.size();
			}
			book.write();
			flag = true;
		}catch(WriteException e){
			System.out.println("写入Excel出错！！"+e);
		}catch(IOException e){
			System.out.println("导出Excel出错！！"+e);
		}finally{
			try{
				if(book!=null){
					book.close();
					book = null;
				}
			}catch(WriteException e){
				e.printStackTrace();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		
		return flag;
	}
}
